package com.mio.web.common;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.record.formula.functions.T;

/**
 * Mio项目一些自定义的工具类
 * 
 * @author super
 *
 */
public class MioUtil {
	public static SimpleDateFormat ymdhmsFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

	/**
	 * 创建目录
	 * 
	 * @param filePath
	 * @return
	 */
	public static File createDir(String filePath) {
		File file = new File(filePath);
		if (!file.exists()) {
			file.mkdirs();
		}
		return file;
	}

	/**
	 * 創建文件
	 * 
	 * @param file
	 * @param fileName
	 */
	public static File createFile(File dir, String fileName) {
		File file = new File(dir, fileName);
		if (!file.exists()) {
			try {
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
				return null;
			}
		}
		return file;
	}

	/**
	 * 获取年月日时分秒字符串
	 * 
	 * @param date
	 * @return
	 */
	public static String getYMDHMSDateTime(Date date) {
		if (date != null) {
			return ymdhmsFormat.format(date);
		}
		return ymdhmsFormat.format(new Date());
	}

	public static boolean listIsEmpty(List list) {
		if (list != null && list.size() > 0) {
			return true;
		}
		return false;
	}

	/**
	 * 获取上传到服务器地址
	 * 
	 * @param request
	 * @param modelName
	 *            模块名称，即保存在upload下的目录名称
	 * @param fileName
	 *            上传文件的名称
	 * @return
	 */
	public static File getUploadFile(HttpServletRequest request, String modelName, String fileName,
			HttpServletResponse response) {
		FileWriter fileWriter = null;
//		try {
//			String logTxt = "mio.txt";
//			String logTxtfilePath = request.getSession().getServletContext().getRealPath("exception");
//			File logDir = new File(logTxtfilePath);
//			if (!logDir.exists()) {
//				logDir.mkdirs();
//			}
//			File exceptionFile = new File(logDir, logTxt);
//			if (!exceptionFile.exists()) {
//				exceptionFile.createNewFile();
//			}
//			fileWriter = new FileWriter(exceptionFile, false); // true代表追加
//		} catch (IOException e1) {
//			e1.printStackTrace();
//		}
		//String temp = request.getSession().getServletContext().getRealPath("");
//		try {
//			String a = temp;
//			int index = a.lastIndexOf("apache7")-1;
//			String upload = "/upload/";
//			if (temp.indexOf("/") == -1) {
//				upload = "\\upload\\";
//			}
//			String dirPath = temp.substring(0, index) + upload + modelName;
//			fileWriter.write("dirPath=" + dirPath + "\n");
//			fileWriter.flush();
//		} catch (IOException e1) {
//			e1.printStackTrace();
//		}
		//int lastIndex = temp.lastIndexOf("MioWeb") - 1;
//		String upload = "/upload/";
//		if (temp.indexOf("/") == -1) {
//			upload = "\\upload\\";
//		}

		//String dirPath = temp.substring(0, lastIndex) + upload + modelName;
		
		String dirPath = "/usr/local/upload/"+modelName;
		File dir = new File(dirPath);
		if (!dir.exists()) {
			dir.mkdirs();
		}
		File targetFile = new File(dir, fileName);
		if (!targetFile.exists()) {
			try {
				targetFile.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return targetFile;
	}

}
